package sites.manager.admin.account;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import sites.manager.common.base.BaseService;
import sites.manager.common.kit.Ret;
import sites.manager.common.kit.SiteInfo;
import sites.manager.common.model.Account;

import java.util.Date;

/**
 * 用户管理
 */
public class AccountService extends BaseService {

    private Account dao = new Account().dao();

    public Ret getAccounts(String userName, String nickName, int page, int limit){
        Ret ret = Ret.ok();

        Kv kv = Kv.by("userName", "%"+userName+"%").set("nickName", "%"+nickName+"%").set("siteId", SiteInfo.siteId);
        Page<Account> accountPage = dao.template("account.getAccounts", kv).paginate(page, limit);

        //组装结果
        ret.setCode("0");
        ret.setData(accountPage.getList());
        ret.setCount(accountPage.getTotalRow());

        return ret;
    }

    /**
     * 根据登录名查询用户
     * @param name	登录名
     * @return
     */
    public Account getByName(String name){
        String sql = dao.getSql("account.getByName");
        return dao.findFirst(sql, name, SiteInfo.siteId);
    }

    public Ret save(Account account){
        Ret ret = Ret.ok();

        Account temp = getByName(account.getUserName());
        if(temp != null){
            ret.setFail();
            ret.setMsg("用户名【"+account.getUserName()+"】已被占用，请设定其他值！");
            return ret;
        }

        account.setPassword(HashKit.md5(account.getPassword()));
        account.setStatus(SiteInfo.statusNormal);
        account.setCreator(getLoggedAccount().getId());
        account.setCreateTime(new Date());
        account.setSiteId(SiteInfo.siteId);
        boolean flag = account.save();

        if(!flag) {
            ret.setFail();
            ret.setMsg("保存失败");
        }
        return ret;
    }

    public Ret updateStruts(int id, boolean flag){
        Ret ret = Ret.create();
        Account account = dao.findById(id);
        if(flag){
            account.setStatus(SiteInfo.statusNormal);
        } else {
            account.setStatus(SiteInfo.statusDel);
        }
        account.setUpdater(getLoggedAccount().getId());
        account.setUpdateTime(new Date());
        boolean t = account.update();
        if(t){
            ret.setOk();
            ret.setMsg("更新成功");
        }
        return ret;
    }

    /**
     * 解锁
     *
     * @param id
     * @return
     */
    public Ret unlock(int id){
        Ret ret = Ret.create();
        Account account = dao.findById(id);
        account.setStatus(SiteInfo.statusNormal);
        account.setUpdater(getLoggedAccount().getId());
        account.setUpdateTime(new Date());
        account.setErrorCount(0);
        boolean t = account.update();
        if(t){
            ret.setOk();
            ret.setMsg("更新成功");
        }
        return ret;
    }

    public Account getById(int id){
        return dao.findById(id);
    }

    public Ret update(Account account){
        Ret ret = Ret.create();
        Account a = getById(account.getId());
        a.setNickName(account.getNickName());
        String pwd = account.getPassword();
        if(StrKit.notBlank(pwd)){
            a.setPassword(HashKit.md5(pwd));
        }
        a.setPhone(account.getPhone());
        a.setEmail(account.getEmail());
        a.setUpdater(getLoggedAccount().getId());
        a.setUpdateTime(new Date());
        boolean flag = a.update();
        if(flag){
            ret.setOk();
            ret.set(SiteInfo.msgKey, "更新成功");
        } else {
            ret.setFail();
            ret.set(SiteInfo.msgKey, "更新失败，请联系管理员！");
        }
        return ret;
    }

    public Ret batchRemove(String ids){
        Ret ret = Ret.create();

        String[] idArray = ids.split(",");
        int num = 0;
        for (int i=0,j=idArray.length; i<j; i++){
            Account temp = getById(Integer.parseInt(idArray[i]));
            temp.setStatus(SiteInfo.statusDel);
            temp.setUpdater(getLoggedAccount().getId());
            temp.setUpdateTime(new Date());
            boolean flag = temp.update();
            if(flag){
                num++;
            }
        }

        if(num > 0){
            ret.setOk();
            ret.setMsg("成功禁用【"+num+"】个用户");
        } else {
            ret.setFail().setMsg("禁用失败，请联系管理员！");
        }

        return ret;
    }

}
